package com.ruoyi.web.controller.gatewayManagement;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.GatewayManagement;
import com.ruoyi.system.service.IGatewayManagementService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 空调配置Controller
 * 
 * @author LGD
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/gatewayManagement/gatewayManagement")
public class GatewayManagementController extends BaseController
{
    @Autowired
    private IGatewayManagementService gatewayManagementService;

    /**
     * 查询空调配置列表
     */
    @PreAuthorize("@ss.hasPermi('gatewayManagement:gatewayManagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(GatewayManagement gatewayManagement)
    {
        startPage();
        List<GatewayManagement> list = gatewayManagementService.selectGatewayManagementList(gatewayManagement);
        System.out.println("---list---"+list);
        return getDataTable(list);
    }

    /**
     * 导出空调配置列表
     */
    @PreAuthorize("@ss.hasPermi('gatewayManagement:gatewayManagement:export')")
    @Log(title = "空调配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GatewayManagement gatewayManagement)
    {
        List<GatewayManagement> list = gatewayManagementService.selectGatewayManagementList(gatewayManagement);
        ExcelUtil<GatewayManagement> util = new ExcelUtil<GatewayManagement>(GatewayManagement.class);
        util.exportExcel(response, list, "空调配置数据");
    }

    /**
     * 获取空调配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('gatewayManagement:gatewayManagement:query')")
    @GetMapping(value = "/{gatewayId}")
    public AjaxResult getInfo(@PathVariable("gatewayId") Long gatewayId)
    {
        return success(gatewayManagementService.selectGatewayManagementByGatewayId(gatewayId));
    }

    /**
     * 新增空调配置
     */
    @PreAuthorize("@ss.hasPermi('gatewayManagement:gatewayManagement:add')")
    @Log(title = "空调配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GatewayManagement gatewayManagement)
    {
        return toAjax(gatewayManagementService.insertGatewayManagement(gatewayManagement));
    }

    /**
     * 修改空调配置
     */
    @PreAuthorize("@ss.hasPermi('gatewayManagement:gatewayManagement:edit')")
    @Log(title = "空调配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GatewayManagement gatewayManagement)
    {
        return toAjax(gatewayManagementService.updateGatewayManagement(gatewayManagement));
    }

    /**
     * 删除空调配置
     */
    @PreAuthorize("@ss.hasPermi('gatewayManagement:gatewayManagement:remove')")
    @Log(title = "空调配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gatewayIds}")
    public AjaxResult remove(@PathVariable Long[] gatewayIds)
    {
        return toAjax(gatewayManagementService.deleteGatewayManagementByGatewayIds(gatewayIds));
    }

    /**
     *   设备上电  触发上报
     */
    @PostMapping("/getMainboardInfo")
    public AjaxResult getMainboardInfo(@RequestBody GatewayManagement gatewayManagement){
        GatewayManagement management = new GatewayManagement();
        management.setSerialNumberId(gatewayManagement.getSerialNumberId());
        List<GatewayManagement> gatewayManagementList = gatewayManagementService.selectGatewayManagementList(management);
        if (gatewayManagementList.size() > 0){
            gatewayManagement.setGatewayId(gatewayManagementList.get(0).getGatewayId());
            return toAjax(gatewayManagementService.updateGatewayManagement(gatewayManagement));
        }
        return toAjax(gatewayManagementService.insertGatewayManagement(gatewayManagement));
    }

    /**
     *  更新数据 : 下发一条 MQTT  所有设备回复
     */
    @PostMapping("/updateMainboardData")
    public AjaxResult updateData(){
        System.out.println("----运行了---");


        return null;
    }
    /**
     *  重启：对选中的板子 下发重启 MQTT
     */
    @PostMapping("/restartMainboard")
    public AjaxResult restartMainboard(@RequestBody Long[] gatewayIds){
        System.out.println("***********" + Arrays.toString(gatewayIds));

        return null;
    }
    /**
     *  迁移应用 服务地址 ：  向选中的 设备下发应用服务地址
     */
    @PostMapping("/migrationService")
    public AjaxResult migrationService(@RequestBody Long[] serialNumberIds){
        System.out.println("---序列号---"+ Arrays.toString(serialNumberIds));


        return null;
    }

}
